<?php

class Core_Controller_Admin_Appearance_Form extends Controller
{

	public function indexAction()
	{
		$req = $this -> _request;
		$table = Nuo::getTable('core@form');
		$select = $table -> select();

		$paginator = $this -> paginator = Zend_Paginator::factory($select);

		$paginator -> setCurrentPageNumber($req -> getParam('page', 1));
	}

	public function addAction()
	{

	}
	
	public function addModelFormAction()
	{
		
	}

	/**
	 * manage menu items of an menu.
	 */
	public function elementAction()
	{
		$req = $this -> _request;
		$formId = $req -> getParam('form_id', 1);

		$table = Nuo::getTable('core@form.element');
		$select = $table -> select() -> where('form_id=?', $formId) -> order('pid') -> order('priority');

		$paginator = $this -> paginator = Zend_Paginator::factory($select);

	}

	public function addElementAction()
	{
		$req = $this -> _request;

		$form = $this -> form = Nuo::getForm('core@admin.form.addelement');

		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$values = $form -> getValues();

			$tableEle = Nuo::getTable('core@form.element');
			$row = $tableEle -> fetchNew();
			$row -> setFromArray($values);
			$spec = array(
					'label' => $values['label'],
					'desc' => $values['desc']
			);
			$row -> spec = serialize($spec);
			$row -> save();

			$this -> redirect('admin/appearance/form/element', array('form_id' => $values['form_id']));
		}

	}

	public function editElementAction()
	{
		$req = $this -> _request;

		$id = $req -> getParam('id', 0);

		$row = Nuo::getTable('core@form.element') -> find($id) -> current();

		$form = $this -> form = Nuo::getForm('core@admin.form.addelement');

		if ($req -> isGet() && is_object($row))
		{
			$spec = $row -> spec ? unserialize($row -> spec) : array();
			$spec = array_merge($row -> toArray(), $spec);
			$form -> setValues($spec);
		}

		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$values = $form -> getValues();

			$row -> setFromArray($values);
			$spec = array(
					'label' => $values['label'],
					'desc' => $values['desc']
			);
			$row -> spec = serialize($spec);
			$row -> save();

			$this -> redirect('admin/appearance/form/element', array('form_id' => $values['form_id']));
		}

	}

}
